package com.fauks.gmall.manager.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.fauks.gmall.bean.PmsProductImage;
import com.fauks.gmall.bean.PmsProductInfo;
import com.fauks.gmall.bean.PmsProductSaleAttr;
import com.fauks.gmall.bean.PmsProductSaleAttrValue;
import com.fauks.gmall.manager.mapper.PmsProductImageMapper;
import com.fauks.gmall.manager.mapper.PmsProductSaleAttrMapper;
import com.fauks.gmall.manager.mapper.PmsProductSaleAttrValueMapper;
import com.fauks.gmall.manager.mapper.ProductMapper;
import com.fauks.gmall.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private PmsProductSaleAttrMapper pmsProductSaleAttrMapper;

    @Autowired
    private PmsProductSaleAttrValueMapper pmsProductSaleAttrValueMapper;

    @Autowired
    private PmsProductImageMapper pmsProductImageMapper;

    @Override
    public List<PmsProductInfo> getSpuList(String catalog3Id) {
        PmsProductInfo productInfo = new PmsProductInfo();
        productInfo.setCatalog3Id(catalog3Id);
        return productMapper.select(productInfo);
    }

    @Override
    public void saveProductInfo(PmsProductInfo productInfo) {
        productMapper.insert(productInfo);
    }

    @Override
    public List<PmsProductSaleAttr> spuSaleAttrList(String spuId) {
        PmsProductSaleAttr productSaleAttr = new PmsProductSaleAttr();
        productSaleAttr.setProductId(spuId);
        List<PmsProductSaleAttr> productSaleAttrList = pmsProductSaleAttrMapper.select(productSaleAttr);
        //查询封装attr value
        for (PmsProductSaleAttr pmsProductSaleAttr : productSaleAttrList) {
            PmsProductSaleAttrValue saleAttrValue = new PmsProductSaleAttrValue();
            saleAttrValue.setProductId(spuId);
            saleAttrValue.setSaleAttrId(pmsProductSaleAttr.getSaleAttrId());
            List<PmsProductSaleAttrValue> attrValueList = pmsProductSaleAttrValueMapper.select(saleAttrValue);
            pmsProductSaleAttr.setSpuSaleAttrValueList(attrValueList);
        }
        return productSaleAttrList;
    }

    @Override
    public List<PmsProductImage> spuImageList(String spuId) {
        PmsProductImage pmsProductImage = new PmsProductImage();
        pmsProductImage.setProductId(spuId);
        return pmsProductImageMapper.select(pmsProductImage);
    }

    @Override
    public List<PmsProductSaleAttr> spuSaleAttrListCheckBySku(String spuId, String skuId) {

        //查询商品所有销售属性
        PmsProductSaleAttr productSaleAttr = new PmsProductSaleAttr();
        productSaleAttr.setProductId(spuId);
        List<PmsProductSaleAttr> productSaleAttrList = pmsProductSaleAttrMapper.select(productSaleAttr);
        //查询销售属性值
        for (PmsProductSaleAttr pmsProductSaleAttr : productSaleAttrList) {
            PmsProductSaleAttrValue productSaleAttrValue = new PmsProductSaleAttrValue();
            productSaleAttr.setProductId(spuId);
            productSaleAttr.setSaleAttrId(pmsProductSaleAttr.getSaleAttrId());
            List<PmsProductSaleAttrValue> valueList = pmsProductSaleAttrValueMapper.select(productSaleAttrValue);
            pmsProductSaleAttr.setSpuSaleAttrValueList(valueList);
        }

        //以上方法查询出该spuId下所有销售属性，然后通过sku的id遍历查询默认选中的销售属性比较繁琐消耗性能
        //可以通过sql语句直接查询商品销售属性以及默认选择的销售属性
        List<PmsProductSaleAttr> spuSaleAttrList = pmsProductSaleAttrMapper.spuSaleAttrListCheckBySku(spuId, skuId);
        return spuSaleAttrList;
    }
}
